package cn.vc.shop.controller;

import cn.vc.auth.entity.po.VUser;
import cn.vc.basicmodule.entity.Result;
import cn.vc.shop.entity.dto.CommodityDto;
import cn.vc.shop.entity.po.Commodity;
import cn.vc.shop.service.CommodityService;
import cn.vc.shop.service.ShopService;
import cn.vc.shop.util.SecurityUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品接口
 */
@RestController
@RequestMapping("/commodity")
public class CommodityController {

    private final CommodityService commodityService;
    private final ShopService shopService;

    @Autowired
    public CommodityController(CommodityService commodityService, ShopService shopService) {
        this.commodityService = commodityService;
        this.shopService = shopService;
    }


    /**
     * 新增商品
     * @param commodityDto 商品属性
     * @return
     */
    @PostMapping
    public Result<?> insert(@RequestBody CommodityDto commodityDto) {
        VUser myUser = SecurityUtil.getUser();
        if (myUser==null) return Result.NOT_LOGGED_IN();

        Commodity commodity = new Commodity();

//        commodity.setShopId(myUser.getId());
//        commodity.setCoverUrl(commodityDto.getCoverUrl());
//        commodity.setName(commodityDto.getName());
//        commodity.setSalesType(commodityDto.getSalesType());
//        commodity.setIntroduceUrls(JSON.toJSONString(commodityDto.getIntroduceUrls()));
//        commodity.setPrice(commodityDto.getPrice());
//        commodity.setLimited(commodityDto.getLimited());
//        commodity.setNum(commodityDto.getNum());
//        commodity.setSellTime(commodityDto.getSellTime());

        BeanUtils.copyProperties(commodityDto, commodity);
        commodity.setId(null);
        commodity.setShopId(myUser.getId());
        commodity.setIntroduceUrls(JSON.toJSONString(commodityDto.getIntroduceUrls()));

        boolean bo = commodityService.insert(commodity);

        return new Result<>(bo, null, null);
    }



    /**
     * 删除商品
     * @param token
     * @param id
     * @return
     */
    @DeleteMapping
    public Result<?> delete(@RequestHeader(value = "token") String token,
                            @RequestParam Long id) {
        VUser myUser = SecurityUtil.getUser();
        if (myUser==null) return Result.NOT_LOGGED_IN();
        boolean bo = commodityService.deleteById(id);
//        return resultBuilder.returnMsg(bo?1:0, null, null);
        return new Result<>(bo, null, null);
    }

    /**
     * 查询全部商品
     */
    @GetMapping
    public Result<?> selectAll() {
        List<Commodity> commodityList = commodityService.selectAll();
        return new Result<>(1, commodityList);
    }

    /**
     * 查询我的全部商品
     */
    @GetMapping("/mycommodities")
    public Result<?> selectMyCommodities() {
        VUser myUser = SecurityUtil.getUser();
        if (myUser==null) return Result.NOT_LOGGED_IN();
        List<CommodityDto> commodityDtoList = commodityService.selectByShopId(myUser.getShopId());
        return new Result<>(1, commodityDtoList);
    }
    /**
     * 查询ID商品
     * @param id 商品ID
     * @return
     */
    @GetMapping("/1")
    public Result<?> selectById(@RequestParam Long id) {
        CommodityDto commodityDto = commodityService.selectById(id);
        return new Result<>(1, commodityDto);
    }

}
